/**
 * (C) 2007 - 2009 Parleys.com NV.
 */
package com.parleys.core.service.impl;

import java.util.ArrayList;
import java.util.Collection;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.parleys.core.domain.Channel;
import com.parleys.core.exception.ChannelNotFoundException;
import com.parleys.core.service.ChannelService;
import com.parleys.core.service.PresentationService;

/**
 * 
 * @author berkay
 *
 */
@Service
public class ChannelServiceImpl implements ChannelService
{

    @Autowired
    private PresentationService presentationService;

    private Channel channel1 = new Channel(1L, "BeJUG", "The BeJUG channel", true);
    private Channel channel2 = new Channel(2L, "NLJUG", "The NLJUG channel", true);

    private Channel channel3 = new Channel(3L, "Devoxx'08", "The Devoxx 2008 channel", true);
    private Channel channel4 = new Channel(4L, "Devoxx'09", "The Devoxx 2009 channel", true);
    private Channel channel5 = new Channel(5L, "Devoxx'10", "The Devoxx 2010 channel", false);

    private Channel channel6 = new Channel(6L, "Test", "Test channel", true);

    private Channel channel7 = new Channel(7L, "Internal", "The internal channel", false);

    @PostConstruct
    public void populateChannels()
    {
        channel1.addPresentation(presentationService.getPresentation(1L));
        channel1.addPresentation(presentationService.getPresentation(2L));
        channel1.addPresentation(presentationService.getPresentation(3L));

        channel2.addPresentation(presentationService.getPresentation(4L));
        channel2.addPresentation(presentationService.getPresentation(5L));

        channel3.addPresentation(presentationService.getPresentation(6L));
        channel3.addPresentation(presentationService.getPresentation(7L));
        channel3.addPresentation(presentationService.getPresentation(8L));

        channel4.addPresentation(presentationService.getPresentation(9L));
        channel4.addPresentation(presentationService.getPresentation(10L));
        channel4.addPresentation(presentationService.getPresentation(11L));
        channel4.addPresentation(presentationService.getPresentation(12L));

        // Channel 5 has no presentations

        channel6.addPresentation(presentationService.getPresentation(13L));

        channel7.addPresentation(presentationService.getPresentation(14L));
        channel7.addPresentation(presentationService.getPresentation(15L));
    }

    @Override
    public Channel getChannel(Long channelId)
    {
        for(Channel channel : getChannels())
        {
            if(channel.getId().equals(channelId))
            {
                return channel;
            }
        }
        throw new ChannelNotFoundException(channelId);
    }

    @Override
    public Collection<Channel> getChannels()
    {
        Collection<Channel> channels = new ArrayList<Channel>();
        channels.add(channel1);
        channels.add(channel2);
        channels.add(channel3);
        channels.add(channel4);
        channels.add(channel5);
        channels.add(channel6);
        channels.add(channel7);
        return channels;
    }

}
